package website.lucaswork.survey.core.converters;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import website.lucaswork.survey.core.utils.DefaultValues;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class StringToLocalDateTimeConverter implements Converter<String, LocalDateTime> {

    @Autowired
    DefaultValues defaultValues;

    @Override
    public LocalDateTime convert(String source) {
        if (StringUtils.isEmpty(source)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(defaultValues.getDefaultDatetimeFormat());
        return LocalDateTime.parse(source, formatter);
    }
}
